import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.*;
public class GenerateTickFileByGroupId {    

    private static String input = new String();
    private static Vector vInput= new Vector(10);
    private static Vector vOut = new Vector(10);
    private static String url = new String ();
    private static String gId = new String();

    public static void main(String[] args) {

	String key ="";
	Hashtable localHashTable = new Hashtable();
	GroupProfileLoader gIdLoader = new GroupProfileLoader();

	gIdLoader.setFileName("C:/myProjects/stock/Data/Group/IndustryGroup.txt");
	gIdLoader.loadProfile();
	localHashTable = gIdLoader.getHashTable();
	gIdLoader.printHashTable(localHashTable);

	Enumeration enum = localHashTable.keys();  	   
	while (enum.hasMoreElements()) {
	    key = (String)enum.nextElement();
	    gId = (key.replaceAll(" ","")).trim();
	
	    url = "https://www.bigcharts.com/custom/datek-com/screener/"+
		"screener.asp?sh=1%2C1%2C1%2C1%2C1%2C1%2C1&pg=vop&cols=0&sort=0"+
		"&sortd=1&numresults=1500&s10=1&s10p1="+gId+"&result.x=62&result.y=6";

	    GenerateTickFileByGroupId myConnecter = new GenerateTickFileByGroupId();
	    myConnecter.connectToUrl(url);
	    vInput = myConnecter.putInVectorByLine(input);
	    vOut = myConnecter.selectUsefulInfo(vInput);
	    myConnecter.printTickListToFile(vOut);
	    //System.out.println("Down File:"+gId+".txt");
	}
    }
    
    private Vector putInVectorByLine (String input) {
	
	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		v.addElement(element);
	    }
	}
	
	return v;
    }
    
    private Vector selectUsefulInfo (Vector v_in) {
	Vector v_out = new Vector (10);
	String text = "";
	Enumeration penum = v_in.elements();

	while ( penum.hasMoreElements()) {
	    text = (String)penum.nextElement();
	    if (text.equals("Symbol")) {
	       break;
	    }
	}

	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (text.startsWith("Previou")) {
		break;
	    }
	    if (!(text.startsWith("<"))) {
		v_out.addElement(text);
	    }
	}

	return v_out;
    }

    private void printUsefulInfo (String input) {
	System.out.print(input);
    }

    private void printVector( Vector inputV) {

	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {   
	   System.out.println(penum.nextElement());
	}
    }

    private void printTickList( Vector inputV) {
	String temp = "";
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp = (String)penum.nextElement();
	    temp = temp.replace('?', ' ');
	    temp = temp.replaceAll("<A HREF=","");
	    temp = temp.replaceAll("/custom/datek-com/screener/screener-charting.asp", "");
	    temp = temp.replaceAll("symb=","");
	    temp = temp.replaceAll("\"","");
	    temp = temp.replaceAll("&sid=", "\t");
	    temp = temp.replaceAll(">","");  
	    //System.out.println(temp.trim());
	}
    }

    private void printTickListToFile( Vector inputV) {
	MyFileWriter myWriter = new MyFileWriter();
	//File myFile = new File("./Data/Group/"+gId+".txt");
	myWriter.setFileName("./Data/Group/"+gId+".txt");

	String temp = "";
	StringBuffer stBuf = new StringBuffer();
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp = (String)penum.nextElement();
	    temp = temp.replace('?', ' ');
	    temp = temp.replaceAll("<A HREF=","");
	    temp = temp.replaceAll("/custom/datek-com/screener/screener-charting.asp", "");
	    temp = temp.replaceAll("symb=","");
	    temp = temp.replaceAll("\"","");
	    temp = temp.replaceAll("&sid=", "\t");
	    temp = temp.replaceAll(">","");  
	    temp = temp.trim();
	    stBuf.append(temp+"\n");
	}

	System.out.print(stBuf.toString());
	//myWriter.useBufferedOutputStream(stBuf.toString());
    }

    private void connectToUrl (String urlAddress) {
        URL url = null;        
	HttpsURLConnection httpsConnection = null;
	String up;
	//up =  encodeAuthorization();

	try {            

	    url = new URL (urlAddress);  

	    try {                
		httpsConnection = (HttpsURLConnection)url.openConnection();                
		httpsConnection.setDoOutput(false);                
		//httpsConnection.setRequestProperty("Authorization", up);
		httpsConnection.connect();                

		// get the HTTPS server's response code                
		System.out.println("Connection Code and Message:" + 
				   httpsConnection.getResponseCode() + 
				   " - " + httpsConnection.getResponseMessage()+"\n");

                // get the input stream of the connection                
		// ie. read the response from the HTTPS server
                
		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpsConnection.getInputStream()));

		StringBuffer buff1= new StringBuffer("");



		input = in.readLine();

		while (input != null) {
		    buff1.append(input);
		    input = in.readLine();
		}

		in.close();            

		input =  buff1.toString();
		input=input.replaceAll("\n","");
		input=input.replaceAll("\t","");
		input=input.replaceAll("&nbsp;","");
		input=input.replaceAll(">",">\n");
		input=input.replaceAll("</","\n</");

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	    System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }


    private static String encodeAuthorization () {
	// method to encode the username/password

	String authorization = "LI34276" + ":" + "********";
	sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
	return "Basic " + enCoder.encode(authorization.getBytes());
    }
}






